#!/bin/sh /etc/rc.common

START=99
OSCAM_PATH=/usr/bin

[ -f  /tmp/oscam ] && 
{
	OSCAM_PATH=/tmp
	[ -x  /tmp/oscam ] ||  chmod +x /tmp/oscam
}

start() {
	enable=$(uci get oscam.config.enabled 2>/dev/null)
	kill -9 $(ps | grep oscam-watchdog.sh | grep -v "grep" | awk '{print $1}') >/dev/null 2>&1
	if [ $enable -eq 1 ]; then
		/etc/init.d/pcscd enable
		/etc/init.d/pcscd start
		if pidof oscam >/dev/null; then
			kill $(pidof oscam) >/dev/null 2>&1 || killall -9 oscam >/dev/null 2>&1
			sleep 1
		fi
	echo "enable"
	[ -d /var/log/oscam ] || mkdir -p /var/log/oscam
	service_start ${OSCAM_PATH}/oscam -b -r 2 -u
	nohup /usr/share/oscam/oscam-watchdog.sh > /var/log/oscam.log 2>&1 &
	fi
}

stop() {
	enable=$(uci get oscam.config.enabled 2>/dev/null)
	if [ $enable -ne 1 ]; then
		echo "stop!"
		kill -9 $(ps | grep oscam-watchdog.sh | grep -v "grep" | awk '{print $1}') >/dev/null 2>&1
		kill $(pidof oscam) >/dev/null 2>&1 || killall -9 oscam >/dev/null 2>&1
		/etc/init.d/pcscd stop 
		/etc/init.d/pcscd disable
	fi
}

restart(){
	stop
	sleep 2
	start
	echo "oscam Server has restarted."
}